﻿using BCCommon;
using BCData.OA.BusinessFlowCategory;
using BCDto.OA.BusinessFlowCategory;
using BCEntity.OA.BusinessFlowCategory;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.OA.BusinessFlowCategory
{
    public class BusinessFlowCategoryService : IBusinessFlowCategoryService
    {
        private readonly IApplicationContextService applicationContextService;
        private readonly IBusinessFlowCategoryData businessFlowCategoryData;

        public BusinessFlowCategoryService(IApplicationContextService applicationContextService,IBusinessFlowCategoryData businessFlowCategoryData)
        {
            this.applicationContextService = applicationContextService;
            this.businessFlowCategoryData = businessFlowCategoryData;
        }

        public bool Exists(long businessFlowCategoryId)
        {
            return this.businessFlowCategoryData.Exists(businessFlowCategoryId).GetAwaiter().GetResult();
        }

        public bool Exists(string businessFlowCategoryName)
        {
            return this.businessFlowCategoryData.Exists(businessFlowCategoryName.Trim()).GetAwaiter().GetResult();
        }

        public BusinessFlowCategoryDto Add(BusinessFlowCategoryRequestDto requestDto)
        {
            if (this.businessFlowCategoryData.Exists(requestDto.BusinessFlowCategoryName.Trim()).GetAwaiter().GetResult())
            {
                throw new ArgumentException("OA系统业务流分类名称已存在");
            }
            var entity = requestDto.As<BusinessFlowCategoryEntity>();
            entity.CompanyId = applicationContextService.Admin.CompanyId;
            entity.CreateAdminName = applicationContextService.Admin.FullName;
            entity.CreateAdminId = applicationContextService.Admin.AdminId;

            return this.businessFlowCategoryData.Add(entity).GetAwaiter().GetResult().As<BusinessFlowCategoryDto>();
        }
        public BusinessFlowCategoryDto Update(BusinessFlowCategoryPutDto putDto)
        {
            if (!this.Exists(putDto.BusinessFlowCategoryId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var oldEntity = this.businessFlowCategoryData.GetEntity(putDto.BusinessFlowCategoryId).GetAwaiter().GetResult();
            if (oldEntity.BusinessFlowCategoryName != putDto.BusinessFlowCategoryName)
            {
                if (this.businessFlowCategoryData.Exists(putDto.BusinessFlowCategoryName.Trim()).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("OA系统业务流分类名称已存在");
                }
            }
            oldEntity.BusinessFlowCategoryName = putDto.BusinessFlowCategoryName;
            oldEntity.EditAdminName = applicationContextService.Admin.FullName;
            oldEntity.EditAdminId = applicationContextService.Admin.AdminId;

            return this.businessFlowCategoryData.Update(oldEntity).GetAwaiter().GetResult().As<BusinessFlowCategoryDto>();
        }

        public bool Delete(long businessFlowCategoryId)
        {
            if (!this.Exists(businessFlowCategoryId))
            {
                throw new ArgumentException("此记录不存在");
            }

            return this.businessFlowCategoryData.Delete(businessFlowCategoryId).GetAwaiter().GetResult();
        }

        public BusinessFlowCategoryDto GetEntity(long businessFlowCategoryId)
        {
            return this.businessFlowCategoryData.GetEntity(businessFlowCategoryId).GetAwaiter().GetResult().As<BusinessFlowCategoryDto>();
        }

        public IEnumerable<BusinessFlowCategoryDto> GetList(int companyId)
        {
            return this.businessFlowCategoryData.GetList(companyId).GetAwaiter().GetResult().As<IEnumerable<BusinessFlowCategoryDto>>();
        }

        public PageableList<BusinessFlowCategoryDto> Query(int companyId, string businessFlowCategoryName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.businessFlowCategoryData.Query(companyId, businessFlowCategoryName, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<BusinessFlowCategoryDto>
            {
                Items = list.As<IEnumerable<BusinessFlowCategoryDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
